<?php

namespace app\admin\controller\health\tracking;

use app\common\controller\Backend;
use app\health\model\HealthEvaluationClick;

class Evaluation extends Backend
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $where  = [];
            if (!empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) === 2) {
                $where['create_time'] = ['between time', [$params['created_at'][0] . ' 00:00:00', $params['created_at'][1] . ' 23:59:59']];
            }
            $paginate = HealthEvaluationClick::where($where)
                ->with(['evaluation'])
                ->field('count(id) as total_num,code')
                ->group('code,user_id')
                ->select();
            $lists    = [];
            foreach ($paginate as $item) {
                if (isset($lists[$item->code])) {
                    $lists[$item->code]['total_num']  += $item->total_num;
                    $lists[$item->code]['total_user'] += 1;
                } else {
                    $lists[$item->code] = [
                        'code'       => $item->code,
                        'name'       => $item->evaluation->name,
                        'total_num'  => $item->total_num,
                        'total_user' => 1
                    ];
                }
            }
            sort($lists);
            $totalNum = array_column($lists, 'total_num'); // 获取年龄列
            array_multisort($totalNum, SORT_DESC, $lists); // 对年龄列进行升序排序
            $data['rows']  = $lists;
            $data['total'] = count($lists);
            $this->success('查看用户', null, $data);
        }
        return $this->view->fetch();
    }
}